(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:FuwuCtrl
     * @description
     * # FuwuCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('FuwuCtrl', FuwuCtrl);

    FuwuCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', 'sessionService', '$interval', '$state'];

    function FuwuCtrl($scope, dataService, helper, $rootScope, $location, sessionService, $interval, $state) {

        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getList(1);

            $scope.$on('event:up-store-id', function() {
                $scope.getList(1);
            });
        }

        $scope.getList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    // type: 1,
                }
            };

            dataService.fuwuGet(query).then(function(list) {
                if (list) {
                    $scope.fuwu_list = list.fuwu_list;
                    $scope.fuwu_count = list.total_count;
                }
            });
        }

        //添加页面
        $scope.addAction = function() {
            $scope.fuwu_id = $location.search().fuwu_id;
            $scope.type = $location.search().type;
            $scope.uid = random();
            if ($scope.fuwu_id) {
                getOneData();
                $scope.$on('event:up-store-id', function() {
                    getOneData();
                });
            } else {
                $scope.fuwu_data = {
                    "name": "体验版",
                    "type": 1,
                    "version": 9,
                    "month_fee": 100,
                    "season_fee": 400,
                    "half_year_fee": 600,
                    "year_fee": 1200,
                    "introduce": "活动说明",
                    "remark": "",
                };
            }
        }

        //版本添加
        $scope.fuwuAdd = function() {
            //价格 转换一下格式
            $scope.fuwu_data.month_fee = parseFloat($scope.fuwu_data.month_fee);
            $scope.fuwu_data.season_fee = parseFloat($scope.fuwu_data.season_fee);
            $scope.fuwu_data.half_year_fee = parseFloat($scope.fuwu_data.half_year_fee);
            $scope.fuwu_data.year_fee = parseFloat($scope.fuwu_data.year_fee);

            if ($scope.fuwu_data.month_fee < 0) {
                helper.myAlert("月费不能为负值！");
                return false;
            }

            if ($scope.fuwu_data.season_fee < 0) {
                helper.myAlert("季费不能为负值！");
                return false;
            }

            if ($scope.fuwu_data.half_year_fee < 0) {
                helper.myAlert("半年不能为负值！");
                return false;
            }

            if ($scope.fuwu_data.year_fee < 0) {
                helper.myAlert("年费不能为负值！");
                return false;
            }

            if ($scope.fuwu_id) {
                //console.log("fuwu_data-->", $scope.fuwu_data);
                // return false;
                dataService.fuwuUpdate($scope.fuwu_data).then(function(result) {
                    if (result.fuwu_id) {
                        helper.myAlert("success", "#/fuwu/list");
                    } else {
                        helper.myAlert(result.Msg);
                    }
                });
            } else {
                dataService.fuwuAdd($scope.fuwu_data).then(function(result) {
                    if (result.fuwu_id) {
                        helper.myAlert("success", "#/fuwu/list");
                    } else {
                        helper.myAlert(result.Msg);
                    }
                });
            }
        }

        $scope.userdata = function(memberInfo) {
            $scope.manual_data.user_id = memberInfo.user_id;
            $scope.seller_nick = memberInfo.nick_name;
        }

        $scope.distributor = function(memberInfo){
            $scope.manual_data.distributor_id = memberInfo.user_id;
            $scope.distributor_name = memberInfo.nick_name;
        }

        //手工创建服务订单
        $scope.manualordersAction = function() {
            $scope.manual_data = {
                "type": "manual",
                "name": "体验版",
                "version": 9,
                "fee": 100,
                "order_cycle_start": $scope.getDateStr(0),
                "order_cycle_end": $scope.getDateStr(7),
            }
        }

        $scope.manualorderAdd = function() {
            console.error("manual_data-->", $scope.manual_data);
            if ($scope.manual_data.user_id) {
                dataService.fuwuManualOrderAdd($scope.manual_data).then(function(result) {
                    if (result.fuwu_order_id) {
                        helper.myAlert("success", "#/fuwu/list");
                    } else {
                        helper.myAlert(result.Msg);
                    }
                });
            }else{
                helper.myAlert("没有选择卖家！");
                return false;
            }
        }

        function getOneData() {
            var query = {
                content: {
                    fuwu_id: $scope.fuwu_id,
                }
            };

            if ($scope.type) {
                query.content.type = $scope.type;
            }

            dataService.fuwuGet(query).then(function(result) {
                if (result.total_count > 0) {
                    $scope.fuwu_data = result.fuwu_list[0];
                    //console.error("fuwu_data",$scope.fuwu_data);
                    $scope.fuwu_data.introduce = json2html(JSON.parse($scope.fuwu_data.introduce));
                } else {
                    helper.myAlert("error：没有查询出服务数据。");
                    return false;
                }
            });
        }

        //服务订单
        $scope.orderAction = function() {
            $scope.pageSize = 10;
            $scope.fuwu_id = $location.search().fuwu_id;
            $scope.chosedStatus = "all";

            $scope.getOrderList(1);
        }

        $scope.getOrderList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    fuwu_id: $scope.fuwu_id,
                }
            };

            if ($scope.chosedStatus && $scope.chosedStatus != "all") {
                if ($scope.chosedStatus == "weizhifu") {
                    query.content.order_status = 1;
                }
                if ($scope.chosedStatus == "yizhifu") {
                    query.content.order_status = 2;
                }
                if ($scope.chosedStatus == "zuofei") {
                    query.content.order_status = 10;
                }
            }

            dataService.fuwuOrderGet(query).then(function(list) {
                if (list) {
                    $scope.role = list.role;
                    $scope.order_list = list.order_list;
                    $scope.order_count = list.order_count;
                }
            });
        }

        //取消订单
        $scope.cancleOrder = function(order_id) {
            helper.myConfirm("确定取消订单么？", function() {
                if (!order_id) {
                    helper.myAlert("订单编号不正确！");
                    return false;
                }
                dataService.cancleOrder(order_id).then(function(result) {
                    if (result.Msg == "ok") {
                        helper.myAlert("success", null, null, function() {
                            $state.go("fuwu.order", null, {
                                reload: true
                            });
                        });
                    } else {
                        helper.myAlert(result.Msg);
                        return false;
                    }
                });
            });
        }

        //代理订单列表
        $scope.detailAction = function() {
            $scope.pageSize = 10;
            $scope.chosedStatus = 'all';
            $scope.getDetailList(1);
        }

        $scope.getDetailList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                }
            };

            if ($scope.chosedStatus && $scope.chosedStatus != "all") {
                if ($scope.chosedStatus == 'weishenhe') {
                    query.content.status = 1;
                }
                if ($scope.chosedStatus == 'daijiesuan') {
                    query.content.status = 2;
                }
                if ($scope.chosedStatus == 'yijiesuan') {
                    query.content.status = 3;
                }
            }

            dataService.fuwuDetailGet(query).then(function(list) {
                if (list) {
                    $scope.detail_list = list.detail_list;
                    $scope.detail_count = list.total_count;
                }
            });
        }

        //审核未结算订单
        $scope.shenhe = function(detail_id) {
            helper.myConfirm('confirm operation', function() {
                var query = {
                    content: {
                        detail_id: detail_id,
                    }
                };

                dataService.fuwuDetailUpdate(query).then(function(r) {
                    if (r.Msg == "ok") {
                        helper.myAlert("success", null, null, function() {
                            $state.go("fuwu.detail", null, {
                                reload: true
                            });
                        });
                    } else {
                        helper.myAlert("error：" + r.Msg);
                    }
                });
            });
        }

        //提现列表
        $scope.clearingAction = function() {
            $scope.pageSize = 10;
            $scope.chosedClearingStatus = 'all';
            $scope.getClearingList(1);
        }

        $scope.getClearingList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                }
            };

            if ($scope.chosedClearingStatus && $scope.chosedClearingStatus != "all") {
                if ($scope.chosedClearingStatus == 'daishenhe') {
                    query.content.status = 1;
                }
                if ($scope.chosedClearingStatus == 'daizhifu') {
                    query.content.status = 2;
                }
                if ($scope.chosedClearingStatus == 'yizhifu') {
                    query.content.status = 3;
                }
            }

            dataService.fuwuClearingGet(query).then(function(list) {
                if (list) {
                    $scope.clearing_list = list.clearing_list;
                    $scope.clearing_count = list.total_count;
                }
            });
        }

        //审核结算
        $scope.shOrder = function(clearing_id) {
            helper.myConfirm('confirm operation', function() {
                var query = {
                    content: {
                        clearing_id: clearing_id,
                        status: 2,
                    }
                };

                dataService.fuwuClearingUpdate(query).then(function(r) {
                    if (r.Msg == "ok") {
                        helper.myAlert("success", null, null, function() {
                            $scope.getClearingList();
                        });
                    } else {
                        helper.myAlert("error：" + r.Msg);
                    }
                });
            });
        }

        //支付结算记录
        $scope.zfOrder = function(clearing_id) {
            helper.myConfirm('confirm operation', function() {
                var query = {
                    content: {
                        clearing_id: clearing_id,
                        status: 3,
                    }
                };

                dataService.fuwuClearingUpdate(query).then(function(r) {
                    if (r.Msg == "ok") {
                        helper.myAlert("success", null, null, function() {
                            $scope.getClearingList();
                        });
                    } else {
                        helper.myAlert("error：" + r.Msg);
                    }
                });
            });
        }

        //结算明细记录
        $scope.clearingdetailAction = function() {
            $scope.pageSize = 10;
            $scope.clearing_id = $location.search().clearing_id;
            if ($scope.clearing_id) {
                $scope.getClearingDetailList();
            }
        }

        //查询结算明细
        $scope.getClearingDetailList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id,
                    clearing_id: $scope.clearing_id,
                }
            };

            dataService.fuwuClearingDetailGet(query).then(function(r) {
                if (r.clearing_detail_list) {
                    $scope.clearing_detail_list = r.clearing_detail_list;
                    $scope.clearing_list_count = r.total_count;
                }
            });
        }

        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            var s = dd.getSeconds();

            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }
    }
})();